package com.ezops.poc.processor;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ezops.poc.bo.TradeStatus;
import com.ezops.poc.dao.ClientModelDAO;
import com.ezops.poc.dao.DataExceptionDAO;
import com.ezops.poc.validations.EzopsValidationException;

/**
 * Processor to handle the validation errors.
 * 
 * @author vgaurav
 * 
 */
@Component(value = "validationErrorProcessor")
public class ValidationErrorProcessor implements Processor {

	@Autowired
	private DataExceptionDAO dao;

	@Autowired
	private ClientModelDAO clientDao;

	/**
	 * Persis the exception which occured during validations.
	 */
	public void process(Exchange exchange) throws Exception {
		Exception exception = (Exception) exchange
				.getProperty(Exchange.EXCEPTION_CAUGHT);
		if (exception instanceof EzopsValidationException) {
			dao.persist((EzopsValidationException) exception);
			// Mark the trade as failed
			clientDao.update(((EzopsValidationException) exception)
					.getTradeIdentifier().getEzopsId(), TradeStatus.FAILED);
		}

	}
	

}
